
cd "../replication-package"

/*


  This do-file cleans state-level variables later to be merged with CPS indiv/couple-level data
  
    
*/

***---------------------***
*** 1. Load the dataset ***
***---------------------***
use "data/state/state_indicators_all.dta", clear 

label var year "Year"
drop if state_a==11
drop if state_a > 56 | state_a==.
tab state_a
tab year

drop if statefip==11 // drop DC

***** Keep years of interest 
keep if inrange(year, 1982, 2019)	

*****--------------------------------------*****
***** Step 2. Merge with the GSS variables *****
*****--------------------------------------*****
// this dataset was extracted using the GSS restricted dataset 
// only goes up to 2018. 2019 filled with 2018
// see "CPSST_codes/_state/04_value_gss.do"
// regional-decade already filled (*_n)

// generate gender role attitude variables 
alpha mwork_whome_n kids_suffer_n presch_suffer_n, gen(genroleatt_main)
label var genroleatt_main "GRA; decade X state, imputed using reg when nec."

alpha mwork_whome_pub_m kids_suffer_pub_m presch_suffer_pub_m, gen(genroleatt_reg)
label var genroleatt_reg "GRA; region X year"

alpha mwork_whome_ch kids_suffer_ch presch_suffer_ch, gen(genroleatt_state)
label var genroleatt_state "GRA; state-constant (Charles et al.)"

// because gender role attitude variables were not collected 1982-1984, use 1985 to fill in 
// and use 2018 to fill in 2019, since GSS not available 2019-2020 
// only the public version - because all the other versions either use decade or all-year mean 
qui levelsof state_a, local(statelist)
foreach st of local statelist {
	qui sum genroleatt_reg if state_a==`st' & year==1985
	replace genroleatt_reg = r(mean) if state_a==`st' & inrange(year, 1982, 1984)
	
	qui sum genroleatt_reg if state_a==`st' & year==2018 
	replace genroleatt_reg = r(mean) if state_a==`st' & year==2019
	
}

rename abany_n abany_main 
rename abany_pub_m abany_reg 
rename abany_ch abany_state

label var abany_main "Abortion att; decade X state, imputed using reg when nec."
label var abany_reg "Abortion att; region X year"
label var abany_state "Abortion att; state-constant (Charles et al.)"


// genearate region variable to merge with GSS religiosity 
gen region = .
decode state_a, gen(state_abbrev_var)
// New England 
replace region = 1 if state_abbrev_var == "CT" | state_abbrev_var== "ME" | state_abbrev_var== "MA" | state_abbrev_var== "NH" | state_abbrev_var== "RI" | state_abbrev_var== "VT"
// MID-ATLANTIC
replace region =2 if state_abbrev_var== "NJ" | state_abbrev_var== "NY" | state_abbrev_var== "PA"
// MIDWEST - East north central
replace region =3 if state_abbrev_var== "IL" | state_abbrev_var== "IN" | state_abbrev_var== "MI" | state_abbrev_var== "OH" | state_abbrev_var== "WI"	
// MIDWEST - West north central
replace region = 4 if state_abbrev_var== "IA" | state_abbrev_var== "KS" | state_abbrev_var== "MN" | state_abbrev_var== "ND" | state_abbrev_var== "SD" | state_abbrev_var== "NE" | state_abbrev_var== "MO"
// SOUTH ATLANTIC
replace region = 5 if state_abbrev_var== "DE" | state_abbrev_var== "FL" | state_abbrev_var== "GA" | state_abbrev_var== "MD" | state_abbrev_var== "NC" | state_abbrev_var== "SC" | state_abbrev_var== "VA" | state_abbrev_var== "DC" | state_abbrev_var== "WV"	
// SOUTH - EAST CENTRAL 
replace region = 6 if state_abbrev_var== "AL" | state_abbrev_var== "KY" | state_abbrev_var== "MS" |  state_abbrev_var== "TN" 
// SOUTH - WEST CENTRAL 
replace region = 7 if state_abbrev_var== "AR" | state_abbrev_var== "LA" | state_abbrev_var== "OK" |  state_abbrev_var== "TX" 
// MOUNTAIN WEST
replace region = 8 if state_abbrev_var== "AZ" | state_abbrev_var== "CO" | state_abbrev_var== "ID" | state_abbrev_var== "MT" | state_abbrev_var== "NV" | state_abbrev_var== "NM" | state_abbrev_var== "UT" | state_abbrev_var== "WY"
// WEST PACIFIC
replace region = 9 if state_abbrev_var== "AK" | state_abbrev_var== "CA" | state_abbrev_var== "HI" | state_abbrev_var== "OR" | state_abbrev_var== "WA"

label define region_lab 1 "New Eng" 2 "Mid Atlantic" 3 "East North Central" 4 "West North Central" 5 "South Atlantic" 6 "East South Central" 7 "West South Central" 8 "Mountain" 9 "Pacific" , replace
label values region region_lab
tab region 


*****--------------*****
***** Step 6. Save *****
*****--------------*****
// note that standardization and mapping should be done AFTER matching with the main data 
// because we want to show the pattern for our sample only. 
keep state_a year earngap* *wagegap* lfrate* mf60ratio_earn mf60ratio_wage* gss_decade_miss genroleatt_* abany_* pct_maleleg *_cps evanpro* wrkhr_return poverty_gap
keep if inrange(year, 1982, 2019)
rename year year4 // to merge with the CPS couple-level data 


save "data/state/state_level_context.dta", replace




